import { createAsyncThunk , createSlice } from "@reduxjs/toolkit";

import {getMovieListRequest} from '../../api/movieRequest'

//创建异步action
export const getMovieAction = createAsyncThunk('movie/getMovieList', async()=>{
    const re = await  getMovieListRequest()
    return re.data.data.list
}) 

const movieSlice = createSlice({
    name:'movie',
    initialState:{
        movieList:[]
    },
    reducers:{

    },
    //异步reducer需要在extraReducers里
    extraReducers:{
        //promise的状态监听
        [getMovieAction.fulfilled](prev,action){
            console.log("异步action请求成功--getMovieAction");
            prev.movieList=action.payload
        },
        [getMovieAction.rejected](prev,action){
            console.log("异步action请求失败--getMovieAction");
        },
        [getMovieAction.pending](prev,action){
            console.log("异步action请求进行中--getMovieAction");
         
        }
    }
})

export default movieSlice.reducer